package com.hbvtc.xxxy.nikeSale.frames;

/*
 * 修改商品信息
 */
import java.awt.Font;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.hbvtc.xxxy.nikeSale.utils.DbManager;

public class UpdateShopNike extends JFrame {
	JLabel lalId, lalPrice, lalStock;
	JTextField txtPrice, txtStock;
	List list;
	JButton btnUpdate;
	Connection conn;
	PreparedStatement pst;
	ResultSet rs;
	String sql1, sql2;

	public UpdateShopNike() {
		this.setTitle("商品信息修改界面");
		this.setSize(1000, 700);
		this.setLocationRelativeTo(null);
		this.setVisible(true);

		// 设置标题栏的图标
		ImageIcon imageIcon = new ImageIcon(getClass().getResource("01.png"));
		this.setIconImage(imageIcon.getImage());
		// 背景图片的路径
		String path = "images/001.jpg";
		ImageIcon background = new ImageIcon(path);
		// 把背景图片显示在一个标签里面
		JLabel label = new JLabel(background);
		// 把标签的大小位置设置为图片刚好填充整个面板
		label.setBounds(0, 0, this.getWidth(), this.getHeight());
		// 把内容窗格转化为JPanel，否则不能用方法setOpaque()来使内容窗格透明
		JPanel imagePanel = (JPanel) this.getContentPane();
		imagePanel.setOpaque(false);
		// 把背景图片添加到分层窗格的最底层作为背景
		this.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
		
		this.setLayout(null);
		lalId = new JLabel("请选择商品编号：");
		lalPrice = new JLabel("商品的合适价格：");
		lalStock = new JLabel("商品库存数：");
		txtPrice = new JTextField();
		txtStock = new JTextField();
		btnUpdate = new JButton("立即修改");
		// 用来进行商品的选择
		list = new List();
		conn = DbManager.getconn();
		sql1 = "select GoodsID from goods";
		try {
			pst = conn.prepareStatement(sql1);
			rs = pst.executeQuery();
			while (rs.next()) {
				list.add(String.valueOf(rs.getInt("GoodsID")));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setLayout(null);
		this.add(lalId);
		this.add(lalPrice);
		this.add(lalStock);
		this.add(txtPrice);
		this.add(txtStock);
		this.add(list);
		this.add(btnUpdate);

		list.setBounds(400, 150, 280, 30);
		list.setFont(new Font(null, 0, 24));
		lalId.setBounds(150, 150, 200, 30);
		lalId.setFont(new Font(null, 0, 24));
		lalPrice.setBounds(150, 300, 200, 30);
		lalPrice.setFont(new Font(null, 0, 24));
		lalStock.setBounds(150, 470, 200, 30);
		lalStock.setFont(new Font(null, 0, 24));
		txtPrice.setBounds(400, 300, 280, 30);
		txtPrice.setFont(new Font(null, 0, 24));
		txtStock.setBounds(400, 470, 280, 30);
		txtStock.setFont(new Font(null, 0, 24));
		btnUpdate.setBounds(650, 550, 150, 40);
		btnUpdate.setFont(new Font(null, 0, 24));

		btnUpdate.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				int Id = Integer.parseInt(list.getSelectedItem());
				int Price = Integer.parseInt(txtPrice.getText().toString());
				int Stock = Integer.parseInt(txtStock.getText().toString());
				sql2 = "update goods set SalePrice=?,StockNum=? where GoodsID=?";
				try {
					pst = conn.prepareStatement(sql2);
					pst.setInt(1, Price);
					pst.setInt(2, Stock);
					pst.setInt(3, Id);
					pst.execute();
					JOptionPane.showMessageDialog(UpdateShopNike.this, "修改成功");
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
	}

	public static void main(String[] args) {
		new UpdateShopNike();
	}
}
